/*
 * CreateRevision.java
 *
 * Created on 16 de julio de 2007, 19:44
 */

package Interfaz;

import java.awt.CardLayout;

/**
 *
 * @author  Ignacio
 */
public class CreateRevision extends javax.swing.JDialog {
    private AuthorsPanel authorsPanel;
    private int revisionNumber;
    private java.util.Date date;
    /** Creates new form CreateRevision */
    public CreateRevision(java.awt.Frame parent, boolean modal, int revisionNumber, java.util.Date date ) {
        super(parent, modal);
        initComponents();
        authorsPanel = new AuthorsPanel();
        this.panel.add( authorsPanel );
        this.panel.setLayout( new CardLayout() );
        this.authorsPanel.setVisible( true );
        
        this.revisionNumber = revisionNumber;
        this.date = date;
        
        this.tRevision.setText( "" + revisionNumber );
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "dd/MM/yyyy" );
        this.tDate.setText( sdf.format( date ) );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lRevision = new javax.swing.JLabel();
        lDate = new javax.swing.JLabel();
        lDescription = new javax.swing.JLabel();
        tRevision = new javax.swing.JTextField();
        tDate = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tDescription = new javax.swing.JTextArea();
        panel = new javax.swing.JPanel();
        bOk = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        lRevision.setText("Revision");

        lDate.setText("Date");

        lDescription.setText("Description");

        tRevision.setEditable(false);

        tDate.setEditable(false);

        tDescription.setColumns(20);
        tDescription.setLineWrap(true);
        tDescription.setRows(5);
        tDescription.setWrapStyleWord(true);
        jScrollPane1.setViewportView(tDescription);

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 388, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
        );

        bOk.setText("Ok");
        bOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOkActionPerformed(evt);
            }
        });

        bCancel.setText("Cancel");
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(lDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lRevision, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tDate)
                                .addComponent(tRevision, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bOk, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lRevision)
                    .addComponent(tRevision, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lDate)
                    .addComponent(tDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lDescription)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancel)
                    .addComponent(bOk))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
        setVisible( false );
    }//GEN-LAST:event_bCancelActionPerformed

    public boolean validar() {
        if( tDescription.getDocument().getLength() == 0 )
        {
            Modelo.Util.errorDialog( "Error", "Description is empty" );
            return false;
        }
        
        if( this.authorsPanel.getAuthors().size() == 0 )
        {
            Modelo.Util.errorDialog( "Error", "No authors in the list" );
            return false;
        }
        
        return true;
    }

    public void process()
    {
        validar();
    }
    
    
    private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
        process();
        this.setVisible(false);
    }//GEN-LAST:event_bOkActionPerformed
    
    public Modelo.Revision getRevision() {
        if( validar() )
            return new Modelo.Revision( date, this.authorsPanel.getAuthors(), this.tDescription.getText(), revisionNumber );
        else
            return null;
    }

    public java.util.Date getDate() {
        return date;
    }

    public javax.swing.JLabel getLRevision() {
        return lRevision;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CreateRevision(new javax.swing.JFrame(), true, 1, new java.util.Date()).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bOk;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lDate;
    private javax.swing.JLabel lDescription;
    private javax.swing.JLabel lRevision;
    private javax.swing.JPanel panel;
    private javax.swing.JTextField tDate;
    private javax.swing.JTextArea tDescription;
    private javax.swing.JTextField tRevision;
    // End of variables declaration//GEN-END:variables
    
}
